<?php

namespace app\home\controller;

use think\facade\Request;

class User extends Base {
    
    protected function initialize()
    {
        parent::initialize();
    }
    
    /**
     * @desc 登录
     */
    public function login() {
        return view();
    }
    
    /**
     * @desc 登录操作
     */
    public function loginAjax() {
        $params['username']  = Request::post('username');
        $params['password']  = Request::post('password');
        $params['phone']     = Request::post('phone');
        $params['phoneCode'] = Request::post('phoneCode');
        $params['tag']       = Request::post('tag');
    
        $logic = model('User', 'logic');
        $data = [];
        try {
            $data = $logic->loginAjax($params);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode, $logic->message);
    }
    
    /**
     * @desc 注册
     */
    public function register() {
        return view();
    }
    
    /**
     * @desc 注册操作
     */
    public function registerAjax() {
        $params['phone']     = Request::post('phone');
        $params['phoneCode'] = Request::post('phoneCode');
        $params['password1'] = Request::post('password1');
        $params['email']     = Request::post('email');
        $params['emailCode'] = Request::post('emailCode');
        $params['password2'] = Request::post('password2');
        $params['tag']       = Request::post('tag');
        
        $logic = model('User', 'logic');
        $data = [];
        try {
            $data = $logic->registerAjax($params);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode);
    }
    
    /**
     * @desc 忘记密码
     */
    public function findPassword() {
        return view();
    }
    
    /**
     * @desc 通过手机找回密码
     */
    public function findPasswordByPhone() {
        return view();
    }
    
    /**
     * @desc 通过手机号找回密码
     */
    public function findPasswordByPhoneAjax() {
        $params['phone']     = Request::post('phone');
        $params['phoneCode'] = Request::post('phoneCode');
        $params['password']  = Request::post('password');
        $params['tag']       = Request::post('tag');
        
        $logic = model('User', 'logic');
        $data = [];
        try {
            $data = $logic->findPasswordByPhoneAjax($params);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode);
    }
    
    /**
     * @desc 通过邮箱找回密码
     */
    public function findPasswordByEmail() {
        return view();
    }
    
    /**
     * @desc
     */
    public function findPasswordByEmailAjax() {
        $params['email']     = Request::post('email');
        $params['emailCode'] = Request::post('emailCode');
        $params['password']  = Request::post('password');
        $params['tag']       = Request::post('tag');
        
        $logic = model('User', 'logic');
        $data = [];
        try {
            $data = $logic->findPasswordByEmailAjax($params);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode);
    }
    
    /**
     * @desc 获取手机验证码
     */
    public function getPhoneCode() {
        $params['phone'] = Request::post('phone');
        $params['tag']   = Request::post('tag');
        
        $logic = model('User', 'logic');
        $data = [];
        try {
            $data = $logic->getPhoneCode($params);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode);
    }
    
    /**
     * @desc 获取邮箱验证码
     */
    public function getEmailCode() {
        $params['email'] = Request::post('email');
        $params['tag']   = Request::post('tag');
        
        $logic = model('User', 'logic');
        $data = [];
        try {
            $data = $logic->getEmailCode($params);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode);
    }
    
    /**
     * @desc 退出
     */
    public function logout() {
        header('Content-Type: text/html; charset=utf-8');
        session('userId', null);
        $this->redirect(url('user/login'));
    }
}
